package com.growthsee.rider.wrapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.growthsee.rider.domain.RiderCourse;
import com.growthsee.rider.domain.RiderCourseCategory;
import com.growthsee.rider.domain.dto.RiderCourseCategoryDto;
import com.growthsee.rider.enumeration.YesNoEnum;
import com.growthsee.rider.mapper.RiderCourseCategoryMapper;
import com.growthsee.rider.mapper.RiderCourseMapper;
import com.growthsee.rider.utils.ApplicationContexts;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ycj
 * @Description: 评价活动视图包装类
 * @date 2020/05/24
 **/
public class CourseCategoryWrapper {


    public static CourseCategoryWrapper builder() {
        return new CourseCategoryWrapper();
    }



    public String check(RiderCourseCategory riderCourseCategory){
        RiderCourseCategoryMapper riderCourseCategoryMapper = ApplicationContexts.getBean(RiderCourseCategoryMapper.class);
        LambdaQueryWrapper<RiderCourseCategory> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(RiderCourseCategory::getDeleted, YesNoEnum.NO.getValue());
        if(StringUtils.isNotEmpty(riderCourseCategory.getName())){
            queryWrapper.eq(RiderCourseCategory::getName,riderCourseCategory.getName());
        }
        if(StringUtils.isNotEmpty(riderCourseCategory.getId())){
            queryWrapper.ne(RiderCourseCategory::getId,riderCourseCategory.getId());
        }
        Integer integer = riderCourseCategoryMapper.selectCount(queryWrapper);
        if(integer>0){
            return "课程分类已存在";
        }else return "";
    }



    public void dataHandler(List<RiderCourseCategoryDto> records) {
        if(CollectionUtils.isNotEmpty(records)){
            List<String>ids = records.stream().map(s->s.getId()).distinct().collect(Collectors.toList());
            RiderCourseMapper riderCourseMapper = ApplicationContexts.getBean(RiderCourseMapper.class);
            List<RiderCourse> riderCourses = riderCourseMapper.selectList(Wrappers.<RiderCourse>lambdaQuery().in(RiderCourse::getCategoryId, ids).eq(RiderCourse::getDeleted,YesNoEnum.NO.getValue()));
            records.forEach(v->{
                List<RiderCourse>myCourse = riderCourses.stream().filter(s->StringUtils.equals(s.getCategoryId(),v.getId())).collect(Collectors.toList());
                if(CollectionUtils.isNotEmpty(myCourse)){
                    v.setCourseNum(myCourse.size());
                }
            });
        }
    }
}
